package com.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.entity.ActiveUser;
import com.entity.Article;
import com.service.IArticleService;

/**
 * @项目名称：dream-face
 * @类名称：ArticleController
 * @类描述： 文章处理中心
 * @创建人：YangChao
 * @联系方式：18629233301@163.com
 * @创建时间：2017年3月2日 上午11:38:34
 * @version 1.0.0
 */
@Controller
@RequestMapping(value = "/article")
public class ArticleController extends BaseController {

	@Autowired
	private IArticleService articleService;

	/*
	 * 进入列表页面(需要权限)
	 */
	@RequestMapping(value = "/list")
	public String list(HttpSession session, Model model) {
		try {
			ActiveUser activeUser = (ActiveUser) session.getAttribute("activeUser");
			// Page<ActiveUser> page = PageHelper.startPage(1, 10);
			List<Article> articleList = articleService.getArticleListByUserId(activeUser.getUserid());
			model.addAttribute("articleList", articleList);
			
			return "article/list";
		} catch (Exception e) {
			log.error(e);
		}
		return "error";
	}

	/*
	 * 进入编辑页面（添加||编辑）
	 */
	@RequestMapping(value = "/edit")
	public String edit(Model model, @RequestParam(value = "id", required = false) Integer id) {
		Article article = null;
		try {
			if (id != null) {
				article = articleService.findById(id);
			} else {
				article = new Article();
			}
			model.addAttribute("article", article);
			return "article/edit";
		} catch (Exception e) {
			log.error(e);
		}
		return "error";
	}

	/*
	 * 进行保存或更新操作
	 */
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public String save(Article article) {
		try {
			articleService.saveOrUpdate(article);
			return "redirect:/article/list";
		} catch (Exception e) {
			log.error(e);
		}
		return "error";
	}
}
